﻿Imports System
Imports System.Data

Public MustInherit Class DataAccessLayerFactory

    '/ <summary>
    '/ Defines the DataAccessLayer implemented data provider types.
    '/ </summary>
    Public Enum DataProviderType
        Access
        Odbc
        OleDb
        Oracle
        Sql
        SqLite
    End Enum

    ' Since this class provides only static methods, make the default constructor private to prevent 
    ' instances from being created with "new DataAccessLayerFactory()"
    Private Sub New()
    End Sub

    '/ <summary>
    '/ Constructs a data access layer data provider based on application configuration settings.
    '/ Application configuration file must contain two keys: 
    '/		1. "DataProviderType" key, with one of the DataProviderType enumerator.
    '/		2. "ConnectionString" key, holds the database connection string.
    '/ </summary>
    Public Overloads Shared Function GetDataAccessLayer(ByVal sProvider As String, ByVal sConnection As String) As DataAccessLayerBaseClass
        ' Make sure application configuration file contains required configuration keys
        If System.Configuration.ConfigurationManager.AppSettings(sProvider) Is Nothing Or System.Configuration.ConfigurationManager.AppSettings(sConnection) Is Nothing Then
            Throw New ArgumentNullException("Spécifiez le 'DataProviderType' et la 'ConnectionString' dans le fichier de configuration de l'application.")
        End If
        Dim dataProvider As DataProviderType

        Try
            ' try to parse the data provider type from configuration file
            dataProvider = CType(System.Enum.Parse(GetType(DataProviderType), System.Configuration.ConfigurationManager.AppSettings(sProvider).ToString(), True), DataProviderType)
        Catch
        End Try

        ' return data access layer provider
        Return GetDataAccessLayer(dataProvider, System.Configuration.ConfigurationManager.AppSettings(sConnection).ToString())
    End Function


    '/ <summary>
    '/ Constructs a data access layer based on caller specific data provider.
    '/ Caller of this method must provide the database connection string through ConnectionString property.
    '/ </summary>
    Public Overloads Shared Function GetDataAccessLayer(ByVal dataProviderType As DataProviderType) As DataAccessLayerBaseClass
        Return GetDataAccessLayer(dataProviderType, Nothing)
    End Function


    '/ <summary>
    '/ Constructs a data access layer data provider.
    '/ </summary>
    Public Overloads Shared Function GetDataAccessLayer(ByVal dataProviderType As DataProviderType, ByVal connectionString As String) As DataAccessLayerBaseClass
        Select Case dataProviderType
            Case dataProviderType.Access, dataProviderType.OleDb
                Return New OleDbDataAccessLayer(connectionString)

            Case dataProviderType.Odbc
                Return New OdbcDataAccessLayer(connectionString)

            Case dataProviderType.Oracle
                Return New OracleDataAccessLayer(connectionString)

            Case dataProviderType.Sql
                Return New SqlDataAccessLayer(connectionString)

            Case dataProviderType.SqLite
                Return New SqLiteDataAccessLayer(connectionString)

            Case Else
                Throw New ArgumentException("Le type du [Provider] pour le [Data Access Layer] est invalide.")
        End Select
    End Function
End Class

